const allServices = require('../../controller/mysql')
const { timeHanle, getTime, generateOrderId } = require('../../util/oderNo')
const order = {
    orderCreate: async (req, res) => {
        const sql = `INSERT INTO order_list (user_id, shop_id, shop_count, shop_price, shop_dec, shop_img,shop_name,order_no, create_time, isPay, tag)
        SELECT cart.user_id, shoplist.shop_id, cart.shop_count, shoplist.shop_price, shoplist.shop_dec,shoplist.shop_img, 
        shoplist.shop_name,'${generateOrderId()}','${getTime()}',${req.body.isPay}, shoplist.tag
        FROM cart 
        JOIN shoplist ON cart.shop_id = shoplist.shop_id
        WHERE cart.user_id = ${req.user.user_id} AND cart.shop_id IN (${req.body.arr});`
        allServices.query(sql).then(res1 => {
            console.log(generateOrderId());
            if (res1.affectedRows) {
                res.send({ code: 200, msg: 'success' })
                const sql = `DELETE FROM cart WHERE user_id = ${req.user.user_id} AND shop_id IN (${req.body.arr});`
                allServices.query(sql).then(res1 => {
                    console.log(res1);
                })
            }
            console.log(res1);
        })
    },
    orderGet: (req, res) => {
        const sql = `SELECT create_time, isPay, JSON_ARRAYAGG(
            JSON_OBJECT(
              'order_id', order_id,
              'user_id', user_id,
              'shop_id', shop_id,
              'shop_count', shop_count,
              'shop_price', shop_price,
              'shop_img', shop_img,
              'shop_dec', shop_dec,
              'shop_name', shop_name,
              'order_no', order_no,
              'isPay', isPay
            )
          ) data
        FROM order_list
        WHERE user_id = ?
        GROUP BY create_time, isPay;`;

        allServices.query(sql, [req.user.user_id]).then(result => {
            result.forEach(item => {
                item.data = JSON.parse(item.data)
                item.create_time = timeHanle(item.create_time);
            });
            res.send({
                code: 200,
                msg: 'success',
                res: result
            });
        });
    },
    orderGetRmk: (req, res) => {
        const sql = `SELECT create_time, isPay, JSON_ARRAYAGG(
            JSON_OBJECT(
              'order_id', order_id,
              'user_id', user_id,
              'shop_id', shop_id,
              'shop_count', shop_count,
              'shop_price', shop_price,
              'shop_img', shop_img,
              'shop_dec', shop_dec,
              'shop_name', shop_name,
              'order_no', order_no,
              'isPay', isPay
            )
          ) data
        FROM order_list
        WHERE user_id = ? AND isPay = 1
        GROUP BY create_time, isPay;`;

        allServices.query(sql, [req.user.user_id]).then(result => {
            result.forEach(item => {
                item.data = JSON.parse(item.data)
                item.create_time = timeHanle(item.create_time);
            });
            res.send({
                code: 200,
                msg: 'success',
                res: result
            });
        });
    },
    orderGetNoPay: (req, res) => {
        const sql = `SELECT create_time, isPay, JSON_ARRAYAGG(
            JSON_OBJECT(
              'order_id', order_id,
              'user_id', user_id,
              'shop_id', shop_id,
              'shop_count', shop_count,
              'shop_price', shop_price,
              'shop_img', shop_img,
              'shop_dec', shop_dec,
              'shop_name', shop_name,
              'order_no', order_no,
              'isPay', isPay
            )
          ) data
        FROM order_list
        WHERE user_id = ? AND isPay = 0
        GROUP BY create_time, isPay;`;

        allServices.query(sql, [req.user.user_id]).then(result => {
            result.forEach(item => {
                item.data = JSON.parse(item.data)
                item.create_time = timeHanle(item.create_time);
            });
            res.send({
                code: 200,
                msg: 'success',
                res: result
            });
        });
    },
    orderSubmit: (req, res) => {
        const sql = `SELECT shoplist.shop_name, shoplist.shop_dec, 
        shoplist.shop_price, cart.shop_count, shoplist.shop_img, shoplist.shop_id, cart.cart_id 
        FROM shoplist JOIN cart ON cart.shop_id = shoplist.shop_id 
        WHERE cart.user_id = ? AND shoplist.shop_id IN (${req.body.arr});`;
        allServices.query(sql, [req.user.user_id]).then(result => {
            res.send({
                code: 200,
                msg: 'success',
                res: result
            });
        }).catch(err => {
            console.log(err.message);
        });
    },
    submitRemark: (req, res) => {
        const sql = `INSERT INTO remark (user_id, shop_id, remark, create_time, rate, remark_img) VALUES (?, ?, ?, ?, ?, ?)`;
        const params = [req.user.user_id, req.body.shop_id, req.body.remark, getTime(), req.body.rate, req.body.remark_img];
        allServices.query(sql, params).then(result => {
            if (result.affectedRows) {
                res.send({
                    code: 200,
                    msg: 'success'
                });
            }
        }).catch(err => {
            console.log(err.message);
        });
    },
    remarkList: (req, res) => {
        const sql = `select * from remark where shop_id = ?`
        const sql1 = `select username from user where user_id = ?`
        allServices.query(sql, [req.query.shop_id]).then(result => {
            result.forEach(item => {
                item.create_time = timeHanle(item.create_time);
            })
            res.send({
                code: 200,
                msg: 'success',
                res: result
            })
        })
    }
}

module.exports = order